//
//  HPRwdRecordsViewController.m
//  HPZZTong
//
//  Created by handpay on 14-10-22.
//  Copyright (c) 2014年 handpay. All rights reserved.
//

#import "HPRwdRecordsViewController.h"
#import "HPDateTimeListView.h"
#import "HPRecordItemView.h"
#import <CoreText/CoreText.h>

#define PAGESIZE   @"5"
#define kRecordItemBgTag          1001

@interface HPRwdRecordsViewController (){
    
    int currentPage;
    int totalPage;
    
    IBOutlet HPDateTimeListView *startDate;
    IBOutlet HPDateTimeListView *endDate;
    IBOutlet UIButton *queryButton;
    IBOutlet UILabel *recordNumLabel;
    IBOutlet UIView *bottomView;
    IBOutlet UILabel *pageLabel;
    IBOutlet UIButton *firstPageButton;
    IBOutlet UIButton *prePageButton;
    IBOutlet UIButton *nextPageButton;
    IBOutlet UIButton *lastPageButton;
    
    NSArray* dataArray;
    IBOutlet UILabel *totalAmount;
    IBOutlet UILabel *trueTotalAmount;
}

@property(nonatomic, retain)UIView* recordView;
@property(nonatomic, retain)NSString* totalNum;

- (IBAction)queryClick:(id)sender;
- (IBAction)pageButtonClick:(id)sender;

@end

@implementation HPRwdRecordsViewController

- (void)viewDidLoad {
    self.title = @"提现申请记录";
    [super viewDidLoad];
    
    NSString* nowDate = [Utility getNowDate];
    [startDate setLabel:nowDate];
    [startDate setMode:HPDateTimeListViewModeDate];
    
    [endDate setLabel:nowDate];
    [endDate setMode:HPDateTimeListViewModeDate];
    
    UIImage* normalImage = [[UIImage imageNamed:@"bluebutton.png"]stretchableImageWithLeftCapWidth:9 topCapHeight:0];
    UIImage* hoverImage = [[UIImage imageNamed:@"bluebuttonhover.png"]stretchableImageWithLeftCapWidth:9 topCapHeight:0];
    
    UIImage* unClickImage = [[UIImage imageNamed:@"unclickbutton.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0];
    
    [firstPageButton setBackgroundImage:normalImage forState:UIControlStateNormal];
    [firstPageButton setBackgroundImage:hoverImage forState:UIControlStateHighlighted];
    [firstPageButton setBackgroundImage:unClickImage forState:UIControlStateDisabled];
    
    [prePageButton setBackgroundImage:normalImage forState:UIControlStateNormal];
    [prePageButton setBackgroundImage:hoverImage forState:UIControlStateHighlighted];
    [prePageButton setBackgroundImage:unClickImage forState:UIControlStateDisabled];
    
    [nextPageButton setBackgroundImage:normalImage forState:UIControlStateNormal];
    [nextPageButton setBackgroundImage:hoverImage forState:UIControlStateHighlighted];
    [nextPageButton setBackgroundImage:unClickImage forState:UIControlStateDisabled];
    
    [lastPageButton setBackgroundImage:normalImage forState:UIControlStateNormal];
    [lastPageButton setBackgroundImage:hoverImage forState:UIControlStateHighlighted];
    [lastPageButton setBackgroundImage:unClickImage forState:UIControlStateDisabled];
    
    
    [queryButton setBackgroundImage:normalImage forState:UIControlStateNormal];
    [queryButton setBackgroundImage:hoverImage forState:UIControlStateHighlighted];
    [queryButton setBackgroundImage:unClickImage forState:UIControlStateDisabled];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (void)treatBtn{
    if (totalPage < 2) { // 只有一页或者没有数据，按钮不显示
        bottomView.hidden = YES;
    }else{
        bottomView.hidden = NO;
        prePageButton.enabled = firstPageButton.enabled = (currentPage != 1);
        nextPageButton.enabled = lastPageButton.enabled = (currentPage != totalPage);
        
        UIScrollView* scrollV = (UIScrollView*)self.view;
        CGSize size = scrollV.contentSize;
        CGRect frame = bottomView.frame;
        frame.origin.y = size.height;
        bottomView.frame = frame;
        
        size.height += bottomView.frame.size.height + 10;
        scrollV.contentSize = size;
    }
}
- (void)refreshUI:(NSDictionary*)resp{
    int len = 0;
    NSString* items = [resp objectForKey:@"items"];
    if (![NSString isNilOrEmpty:items]) {
        len = [items length];
    }
    items = [NSString stringWithFormat:@"查询到%@条申请成功记录",items];
    
    NSMutableAttributedString* recordNumString = [[[NSMutableAttributedString alloc]initWithString:items]autorelease];
    [recordNumString setAttributes:@{NSForegroundColorAttributeName: kTitleFontColor} range:NSMakeRange(3, len)];
    recordNumLabel.attributedText = recordNumString;
    
    UIScrollView* scrollView = (UIScrollView*)self.view;
    if (totalPage > 0) {
        totalAmount.hidden = NO;
        trueTotalAmount.hidden = NO;
        
        NSString* applyAmounts = [Utility getFormatMoneyAndSymbol:[resp objectForKey:@"applyAmounts"]];
        NSString* amounts = [Utility getFormatMoneyAndSymbol:[resp objectForKey:@"amounts"]];
        
        NSMutableAttributedString* applyAmountsString = [[[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"申请提现总金额：%@",applyAmounts]]autorelease];
        [applyAmountsString setAttributes:@{NSForegroundColorAttributeName: kTitleFontColor} range:NSMakeRange(8, applyAmounts.length)];
        totalAmount.attributedText = applyAmountsString;
        
        NSMutableAttributedString* amountsString = [[[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"实际提现总金额：%@",amounts]]autorelease];
        [amountsString setAttributes:@{NSForegroundColorAttributeName: kTitleFontColor} range:NSMakeRange(8, amounts.length)];
        trueTotalAmount.attributedText = amountsString;
        
        pageLabel.text = [NSString stringWithFormat:@"第%d页  共%d页", currentPage, totalPage];


        CGSize contentSize = scrollView.frame.size;
        
        CGRect recorFrame = CGRectMake(0, 277, ScreenWidth, 100);
        if (self.recordView) {
            [self.recordView removeFromSuperview];
            self.recordView = nil;
        }
        UIView* recView = [[UIView alloc]initWithFrame:recorFrame];
        self.recordView = recView;
        [self.view addSubview:self.recordView];
        [recView release];
        
        int count = 0;
        if (totalPage == 1) {
            count = [self.totalNum intValue];
        }else{
            if (currentPage == totalPage) {
                count = [self.totalNum intValue] % [PAGESIZE intValue];
                if (count == 0) {
                    count = PAGESIZE.intValue;
                }
            }else{
                count = [PAGESIZE intValue];
            }
        }
        
        int totalH = 10;
        int itemHeight = 0;
        
        for (int i = 0; i < count; i++) {
            HPRecordItemView* itemView = [[HPRecordItemView alloc]initWithFrame:CGRectMake(0, totalH, ScreenWidth, 100) data:[dataArray objectAtIndex:i]];
            itemView.tag = kRecordItemBgTag + i;
            [self.recordView addSubview:itemView];
            [itemView release];
            
            itemHeight = itemView.viewH;
            
            totalH += itemHeight;
        }
        
        recorFrame.size.height = totalH;
        self.recordView.frame = recorFrame;
        contentSize.height = (recorFrame.size.height + recorFrame.origin.y + 10);
        scrollView.contentSize = contentSize;
        [self treatBtn];
    }else{
        totalAmount.hidden = YES;
        trueTotalAmount.hidden = YES;
        
        for (int i = 0; i < PAGESIZE.intValue; i++) {
            if (self.recordView) {
                UIView* tagView = [self.recordView viewWithTag:(kRecordItemBgTag + i)];
                if (tagView) {
                    [tagView removeFromSuperview];
                }

            }
        }
        [HPUIENG showAlert:@"暂无申请记录"];
        if (self.recordView) {
            self.recordView.hidden = YES;
            
        }
        scrollView.contentSize = self.view.frame.size;
    }
}

- (void)responseCallBackSuccess:(NSString *)action withData:(NSDictionary *)respData{
    [HPUIENG removeWaiting];
    if ([action isEqualToString:NET_ACTION_QUERY_RWD_RECORDS]) {
        currentPage = [[respData objectForKey:@"pageNo"] intValue];
        totalPage = [[respData objectForKey:@"totalPages"] intValue];
        NSArray* arr = [respData objectForKey:@"list"];
        if (arr && arr.count >0) {
            dataArray = [[NSArray alloc]initWithArray:arr];
            self.totalNum = [NSString stringWithFormat:@"%d",(int)arr.count];
        }else{
            dataArray = [[NSArray alloc]init];
        }
        
        [HPUIENG removeWaiting];
        [self performSelectorOnMainThread:@selector(refreshUI:) withObject:respData waitUntilDone:YES];
    }
}
- (void)sendRecordRequest:(int)pageIndex{
    NSString* startTime = startDate.value;
    NSString* endTime = endDate.value;
    
    if (![Utility isLegalDate:startTime] || ![Utility isLegalDate:endTime]) {
        [HPUIENG showAlert:@"当前日期查询不到所需信息"];
        return;
    }
    if ([startTime compare:endTime] == NSOrderedDescending) {
        [HPUIENG showAlert:@"结束日期不能小于起始日期"];
        return;
    }
    startTime = [NSString stringWithFormat:@"%@000000", startTime];
    endTime = [NSString stringWithFormat:@"%@235959", endTime];
    
    ZZTongClientEngine* zztEngine = [ZZTongClientEngine sharedInstance];
    NSDictionary* params = @{@"startTime": startTime,
                             @"endTime": endTime,
                             @"pageNo": [NSString stringWithFormat:@"%d", pageIndex],
                             @"pageSize": PAGESIZE,
                             KEY_CHANNEL: [zztEngine getChannel],
                             KEY_ACCOUNT:DES_ENC(ZZT_CLIENG.accountInfo.account)};
    
//    NSDictionary* dic = @{@"pageNo" : @"1",
//                          @"totalPages" : @"3",
//                          @"items" : @"13",
//                          @"list" : @[@{@"rwdTime"   : @"20141018192134",
//                                        @"rwdStatus" : @"00",
//                                        @"rwdMoney"  : @"1000000",
//                                        @"wdMoney"   : @"999996"},
//                                      
//                                      @{@"rwdTime"   : @"20141018192133",
//                                        @"rwdStatus" : @"01",
//                                        @"rwdMoney"  : @"1000000",
//                                        @"wdMoney"   : @"999996"},
//                                      
//                                      @{@"rwdTime"   : @"20141018192132",
//                                        @"rwdStatus" : @"01",
//                                        @"rwdMoney"  : @"1000000",
//                                        @"wdMoney"   : @"999996"},
//                                      
//                                      @{@"rwdTime"   : @"20141018192131",
//                                        @"rwdStatus" : @"00",
//                                        @"rwdMoney"  : @"1000000",
//                                        @"wdMoney"   : @"999996"},
//                                      
//                                      @{@"rwdTime"   : @"20141018192130",
//                                        @"rwdStatus" : @"00",
//                                        @"rwdMoney"  : @"1000000",
//                                        @"wdMoney"   : @"999996"},
//                                      
//                                      @{@"rwdTime"   : @"20141018192129",
//                                        @"rwdStatus" : @"00",
//                                        @"rwdMoney"  : @"1000000",
//                                        @"wdMoney"   : @"999996"},
//                                      
//                                      @{@"rwdTime"   : @"20141018192128",
//                                        @"rwdStatus" : @"01",
//                                        @"rwdMoney"  : @"1000000",
//                                        @"wdMoney"   : @"999996"},
//                                      
//                                      @{@"rwdTime"   : @"20141018192127",
//                                        @"rwdStatus" : @"01",
//                                        @"rwdMoney"  : @"1000000",
//                                        @"wdMoney"   : @"999996"},
//                                      
//                                      @{@"rwdTime"   : @"20141018192126",
//                                        @"rwdStatus" : @"00",
//                                        @"rwdMoney"  : @"1000000",
//                                        @"wdMoney"   : @"999996"},
//                                      
//                                      @{@"rwdTime"   : @"20141018192125",
//                                        @"rwdStatus" : @"00",
//                                        @"rwdMoney"  : @"1000000",
//                                        @"wdMoney"   : @"999996"},
//                                      
//                                      @{@"rwdTime"   : @"20141018192124",
//                                        @"rwdStatus" : @"01",
//                                        @"rwdMoney"  : @"1000000",
//                                        @"wdMoney"   : @"999996"},
//                                      
//                                      
//                                      @{@"rwdTime"   : @"20141018192123",
//                                        @"rwdStatus" : @"01",
//                                        @"rwdMoney"  : @"1000000",
//                                        @"wdMoney"   : @"999996"},
//                                      
//                                      @{@"rwdTime"   : @"20141018192122",
//                                        @"rwdStatus" : @"00",
//                                        @"rwdMoney"  : @"1000000",
//                                        @"wdMoney"   : @"999996"},]};
//    
//    [self responseCallBackSuccess:NET_ACTION_QUERY_RWD_RECORDS withData:dic];
    [zztEngine.netEngine sendRequest:NET_ACTION_QUERY_RWD_RECORDS withParams:params withDelegate:self];
    [zztEngine.uiEngine showNetWaiting];
}
- (void)responseCallBackFailed:(NSString *)action withErrCode:(NSString *)errCode withErrMsg:(NSString *)msg{
    if ([action isEqualToString:NET_ACTION_QUERY_RWD_RECORDS]) {
    }
}

- (void)dealloc {
    [startDate release];
    [endDate release];
    [queryButton release];
    [recordNumLabel release];
    [bottomView release];
    [pageLabel release];
    [firstPageButton release];
    [prePageButton release];
    [nextPageButton release];
    [lastPageButton release];
    self.recordView = nil;
    [dataArray release];
    [totalAmount release];
    [trueTotalAmount release];
    [super dealloc];
}
- (IBAction)queryClick:(id)sender {
    [self sendRecordRequest:1];
}

- (IBAction)pageButtonClick:(id)sender {
    
    UIButton* button = (UIButton*)sender;

    if (button == firstPageButton) {
         [self sendRecordRequest:1];
    }else if(button == prePageButton){
         [self sendRecordRequest:(currentPage - 1)];
    }else if(button == nextPageButton){
         [self sendRecordRequest:(currentPage + 1)];
    }else if(button == lastPageButton){
         [self sendRecordRequest:totalPage];
    }
}
@end
